﻿SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
ALTER TABLE dbo.bht_ASyncMailStock ADD
	LastErrorSend DATETIME NULL
GO

ALTER PROCEDURE [dbo].[bhp_ASyncMailStockTopGet]
AS
BEGIN

	SELECT TOP 1 * FROM bht_ASyncMailStock WHERE SendTime IS NULL AND (LastErrorSend IS NULL OR DATEADD(SECOND, 600, LastErrorSend) < GETDATE()) ORDER BY SetTime

END

GO

CREATE TABLE [dbo].[bht_ASyncMailStockErrors](
	[RequestId] [int] NOT NULL,
	[SetTime] [datetime] NOT NULL,
	[Message] [nvarchar](MAX) NOT NULL,
 CONSTRAINT [PK_bht_ASyncMailStockErrors] PRIMARY KEY CLUSTERED 
(
	[RequestId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE PROCEDURE [dbo].[bhp_ASyncMailStockError]
	@RequestId	INT,
	@ErrorMessage	NVARCHAR(MAX)
AS
BEGIN

	UPDATE
		bht_ASyncMailStock
	SET
		LastErrorSend = GETDATE()
	WHERE
		RequestId = @RequestId

	INSERT INTO [bht_ASyncMailStockErrors] (RequestId, SetTime, [Message]) VALUES (@RequestId, GETDATE(), @ErrorMessage)

END